# Merge pew and ADL 2013
# library(groundhog)
# groundhog.library(tidyverse, "2023-01-01")
library(tidyverse)

# Load data 
pew <- read_csv("output/data/country_religion_percent.csv")
adl <- read_csv("output/data/adl_full.csv")

# Wrangle --------------------
# subset down adl to wave 1 data 
adl_2013 <- filter(adl, wave == 1)

# merge pew 
pew_dominant <- 
  pew %>% 
  mutate(
    Country = tolower(Country) %>% 
      recode(
        `united states` = "usa", 
        `bosnia-herzegovina` = "bosnia and herzegovina",
        `ivory coast` = "cote d'ivoire"
      )
  ) %>% 
  select(
    Country, starts_with("is_max_"),
    percentage_Buddhists, percentage_Christians, 
    percentage_Hindus, percentage_Jews, percentage_Muslims
  ) %>% 
  pivot_longer(
    cols = starts_with("is_"), 
    names_to = "dominant_relig", 
    values_to = "values"
  ) %>% 
  filter(values == 1) %>% 
  mutate(
    dominant_relig = str_replace(dominant_relig, ".*_", "") %>% 
      tolower(),
    dominant_relig = case_when(
      dominant_relig == "buddhists" ~ "buddhist",
      dominant_relig == "christians" ~ "christian",
      dominant_relig == "hindus" ~ "hindu",
      dominant_relig == "jews" ~ "jew",
      dominant_relig == "muslims" ~ "muslim",
      TRUE ~ "other"
    )
  ) %>% 
  select(Country, dominant_relig, starts_with("percentage_"))


adl_2013 <- 
  adl_2013 %>% 
  left_join(pew_dominant, by = c("country" = "Country"))

# clean variables
# for outgroup attitudes: unfavorabel is 1, if recognized and otherwise 
# then 0. If NA or unrecognized, convert to NA.
adl_2013_clean <- 
  adl_2013 %>% 
  mutate(
    # change education to linear categories 
    education = case_when(
      ED_Age %in% c("5-12", "0-12") ~ 1,
      ED_Age == "13-18" ~ 2,
      ED_Age == "19-22" ~ 3,
      ED_Age == "23+" ~ 4,
      TRUE ~ NA_real_
    ) 
  ) %>% 
  # take out ingroup-ingroup sentiments 
  mutate(
    jewspop_noingroup = ifelse(religion == "jewish", NA_integer_, jewspop),
    muslimspop_noingroup = ifelse(religion == "muslim", NA_integer_, muslimspop),
    christianspop_noingroup = ifelse(religion == "christian", NA_integer_, christianspop),
    hinduspop_noingroup = ifelse(religion == "hindu", NA_integer_, hinduspop),
    buddhistspop_noingroup = ifelse(religion == "buddhist", NA_integer_, buddhistspop)
  ) 

adl_2013_clean[adl_2013_clean$country == "usa",]$world_region <- "americas"

write_csv(adl_2013_clean, "output/data/adl2013_outgroup_attitudes.csv")

